#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
import os
import shutil

def is_excepted_file(file_path, filename):
    if os.path.isfile(file_path) and (filename.endswith(".cjo") or filename.endswith(".so")):
        if filename not in ["default.cjo", "libeditorkit_default.so"]:
            return True
    return False

def copy_file(src, dst):
    try:
        shutil.copy(src, dst)
    except IOError as e:
        print("Unable to copy file %s" % e)
    except:
        print("Unexpected error:", sys.exe_info())

def main():
    cur_path = os.getcwd()

    target_path = os.path.join(cur_path, "editorkit")
    if not os.path.exists(target_path):
        os.makedirs(target_path)

    third_party_path1 = os.path.join(cur_path, "entry\\build\\default\\intermediates\\cj\\cj_build\\editorkit\\aarch64-linux-ohos\\release\\editorkit")
    third_party_path2 = os.path.join(cur_path, "entry\\src\\main\\cangjie\\target\\aarch64-linux-ohos\\release\\editorkit")
    third_party_path = third_party_path1 if os.path.exists(third_party_path1) else third_party_path2
    if os.path.exists(third_party_path):
        for filename in os.listdir(third_party_path):
            file_path = os.path.join(third_party_path, filename)
            if is_excepted_file(file_path, filename):
                copy_file(file_path, target_path)

    else:
        print(third_party_path + ": path is not exists.")

    library_path = os.path.join(cur_path, "library")
    if os.path.exists(library_path):
        for filename in os.listdir(library_path):
            file_path = os.path.join(library_path, filename)
            if os.path.isfile(file_path):
                copy_file(file_path, target_path)
    else:
        print(library_path + ": path is not exists.")

if __name__ == "__main__":
    main()


